package com.ycc.controller;

import com.ycc.pojo.Cart;
import com.ycc.pojo.Content;
import com.ycc.pojo.OrderList;
import com.ycc.service.impl.ContentServiceImpl;
import com.ycc.service.impl.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单controller
 * @Author: ycc
 * @Date: 2021/6/27 10:42
 */
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;

    //查看username的全部订单
    @RequestMapping("/lookOrder")
    public String selectAllByUsername(ModelMap modelMap){
        ///获取登录的用户信息
        User user = (org.springframework.security.core.userdetails.User)
                SecurityContextHolder
                        .getContext()
                        .getAuthentication()
                        .getPrincipal();
        String username = user.getUsername();
        List<OrderList> orderLists = orderService.selectAllByUsername(username);
        modelMap.clear();
        modelMap.addAttribute(orderLists);
        return "OrderList";
    }

    //删除订单
    @RequestMapping("/deleteOrder/{id}")
    public String deleteOrder(@PathVariable("id")String id, ModelMap modelMap){
        ///删除此条订单记录
        orderService.deleteByOrderId(id);
        ///删除后还是订单界面
        ///获取登录的用户信息
        User user = (org.springframework.security.core.userdetails.User)
                SecurityContextHolder
                        .getContext()
                        .getAuthentication()
                        .getPrincipal();
        String username = user.getUsername();
        List<OrderList> orderLists = orderService.selectAllByUsername(username);
        modelMap.addAttribute(orderLists);
        return "OrderList";
    }
}
